slf.getUser();
var childid=slf.getSequenceChild();
var coursePageIds='';
//页面初始加载调用
var money='';
var isBuy=false;
var schoolpageID='';
var dataP={};
//未登录取价格标识
var noSchoolId="";
$(function(){
	//初始加载课程包数据
	var parentid='';
	getCourse();
	getCoursePage();
	$("body").on('click', ".buy_classbtn", function() {
		$(".zcc").removeClass("am-hide")
		setTimeout(function(){
			if(slf.checkSign()){
				console.log("已登录")
				//已登录
				console.log("获取最终Id")
				console.log(coursePageIds)
				var childMess=slf.getSequenceChild();
				if(slf.schoolId=='a'||slf.schoolId==undefined||slf.schoolId==''){
					$(".zcc").addClass("am-hide")
					var messPrompt="请先完善孩子信息，是否现在去完善？"
					$("#class_model .am-modal-bd").html(messPrompt)
					$("#class_model").modal({
						closeViaDimmer: false,
						onConfirm: function(options) {
							window.location.href = "personal/child_list.html"
				        },
					});
				}else{
					if(childMess.state==0){//无期望上课时间
						console.log("无孩子期望上课时间")
						$(".zcc").addClass("am-hide")
						$("#class_model .am-modal-bd").html("您还未选择孩子期望上课时间段，是否现在去完善？")
						$("#class_model").modal({
							relatedTarget: this,
							closeViaDimmer: false,
							onConfirm: function(options) {
								window.location.href = "personal/add_child.html?userId="+childMess.userId+"&childrenId="+childMess.childrenId+""; //去其网上可时间  
							}
						})
					}else if(childMess.state==1){//有期望上课时间
						console.log("有孩子期望上课时间")
						buyModel.buyCoursePage({money:money,coursePageIds:coursePageIds});
					}
				}
			}else{
				console.log("未登录")
				//未登录
				$(".zcc").addClass("am-hide")
				var messPrompt="此功能仅对登录用户开放，是否需要登录？"
				$(".operationModalBody").html(messPrompt)
				$("#operationModal").modal({
					closeViaDimmer: false,
					onConfirm: function(options) {
						window.location.href = "login.html"
			        },
			        onCancel: function() {
			          
			        }
				})
			}
		},1)
	});
});
//获取课程
function getCourse(){
	var pageId='';
	if('y'==slf.getQueryStringByName('order')){
		//从预约过来
		getCoursePageToSchool();
	}else{
		//从商城过来
		slf.ajax({
			url:'/system/getPageCourse',
			param:{
				schoolCoursePageId:slf.getQueryStringByName('coursePage')
			},
			success:function(data){
				console.log(data)
				console.log("商城返回课程列表")
				var courseHTML='';
				if(data.res==1&&data.obj.length>0){
					for(var a=0,b=data.obj.length;a<b;a++){
						//展示可见课程
						if(data.obj[a].isVisible=="Y"){
							courseHTML+='<li class="w100b bg-fff radius10 am-padding-sm am-margin-top-sm bottom-boshow3 bg-F8F8F8" onclick="javascript:window.location.href=\'basepkg_details.html?courseId='+data.obj[a].courseId+'&coursePageIds='+coursePageIds+'\'">'+
							'<ul class="am-avg-sm-3">'+
								'<li class="am-text-left baseName">'+data.obj[a].name+'</li>'
								if(data.obj[a].fitAge==undefined||data.obj[a].fitAge==null||data.obj[a].fitAge==''){
									courseHTML+='<li class="am-text-center" style="height:22px;"></li>'
								}else{
									courseHTML+='<li class="am-text-center">适龄：'+data.obj[a].fitAge+'-'+data.obj[a].endAge+'岁</li>'
								}
								/*if(data.obj[a].period==undefined||data.obj[a].period=="undefined"){*/
								courseHTML+='<li class="am-text-right" style="font-size:12px">学习周期:45分钟<i class="am-icon-angle-right am-icon-sm am-margin-left-xs"></i></li>';
								/*}else{
									courseHTML+='<li class="am-text-right">学习周期:'+data.obj[a].period+'周<i class="am-icon-angle-right am-icon-sm am-margin-left-xs"></i></li>';
								}*/
							courseHTML+='</ul>'+
							'</li>';
						}
						
						
					}
				}else{
					courseHTML+='<img src="../img/zwsj.png" alt="img" style="width:85%">'
				}
				$('#courseDetails').html(courseHTML);
			}
		});
	}
	
	
}
//获取课程包
function getCoursePage(){
	if('y'==slf.getQueryStringByName('order')){
		console.log("从预约过来")
		console.log(slf.getQueryStringByName('coursePage'))
		//从预约过来
		slf.ajax({
			url:'/myCoursePage/querySchoolCoursePageId',
			async:false,
			dataType:"json",
			param:{
				coursePageId:slf.getQueryStringByName('coursePage'),
			},
			success:function(data){
				console.log(data)
				console.log("获取转小包id")//购买需要小包id查询是否有孩子信息
				if(data.res==1&&data.obj!=undefined){
					coursePageIds=data.obj;
					dataP={
						schoolCoursePageId:coursePageIds
					}
				}else{
					console.log("转小包id失败")
				}
			},
			error:function(){
				console.log("转小包id失败error")
				coursePageIds=slf.getQueryStringByName('coursePage')
				dataP={
					schoolCoursePageId:null
				}
				noSchoolId=1;
			}
		});
	}else{
		console.log("从商城过来")
		//从商城
		coursePageIds=slf.getQueryStringByName('coursePage')
		dataP={
			schoolCoursePageId:coursePageIds
		}
	}
	var html='';
	slf.ajax({
		url:'/schoolCoursePage/getSchoolCoursePageMoney',
		async:false,
		param:dataP,
		success:function(data){
			console.log(data)
			console.log("获取价格")
			if(data.res==1&&data.obj.length>0){
				if(noSchoolId==1){
					for(var i=0;i<data.obj.length;i++){
						if(data.obj[i].coursePageId==slf.getQueryStringByName('coursePage')){
							html+='<div class="appointLi borB-EF borT-EF">'
							html+='<div class="appointDivs am-g">'  
							html+='<div class="curriculumImg am-u-sm-4 pad-0 am-u-md-3">'       
							if(data.obj[i].coverPicture==undefined||data.obj[i].coverPicture==null||data.obj[i].coverPicture==''){
								var coverImg=slf.defau.courseIcon(data.obj[i].coursePageId)
								html +='<img src="'+coverImg+'" /></div>'
							}else{
								html +='<img src="'+data.obj[i].coverPicture+'" /></div>'
							}         
							html+='<ul class="appointMess am-u-sm-8 pad-0 am-u-md-9">'       
							html+='<li class="titleLi">'          
							html+='<span class="titleDiv" style="width:100%;">'+data.obj[i].name+'</span></li>'             
							html+='<li class="am-margin-top-xs" style="line-height:20px">' 
							if(data.obj[i].briefIntroduction!=undefined){
								html+='<div class="desc">简介：'+data.obj[i].briefIntroduction+'</div>'            
							}
							html+='</li>'
							html+='<li class="titleLi">'          
							html+='<span class="addrs" style="width:100%;">地点：'+data.obj[i].schoolName+'</span></li>'
							html+='</ul></div>'          
				            html+='<div class="classOpen">'     
				            html+='<span class="classTime font-12">销售时间：'+data.obj[i].salesStartDateString+'至'+data.obj[i].salesEndDateString+'</span>'
				            if(data.obj[i].discountPrice==undefined||data.obj[i].discountPrice==null||data.obj[i].discountPrice==''){
				            	money=data.obj[i].price
				            }else{
				            	money=data.obj[i].discountPrice
				            }
				            html+='<span class="classStatus buy_classbtn" style="background:#FFAB2A;width:100px;font-size:12px;padding:0;box-shadow:none">购买本课程包</span>'     
				            html+='</div></div>'    
						}
					}
				}else{
					html+='<div class="appointLi borB-EF borT-EF">'
					html+='<div class="appointDivs am-g">'  
					html+='<div class="curriculumImg am-u-sm-4 pad-0 am-u-md-3">'
					if(data.obj[0].coverPicture==undefined||data.obj[0].coverPicture==null||data.obj[0].coverPicture==''){
						var coverImg=slf.defau.courseIcon(data.obj[0].coursePageId)
						html +='<img src="'+coverImg+'" /></div>'
					}else{
						html +='<img src="'+data.obj[0].coverPicture+'" /></div>'
					}     
					html+='<ul class="appointMess am-u-sm-8 pad-0 am-u-md-9">'       
					html+='<li class="titleLi">'          
					html+='<span class="titleDiv" style="width:100%;">'+data.obj[0].name+'</span></li>'             
					html+='<li class="am-margin-top-xs" style="line-height:20px;">'        
					if(data.obj[0].briefIntroduction!=undefined){
						html+='<div class="desc">简介：'+data.obj[0].briefIntroduction+'</div>'            
					}            
					html+='</li>'
					html+='<li class="titleLi">'          
					html+='<span class="addr" style="width:100%;font-size:12px;color:#888">上课地点：'+data.obj[0].schoolName+'</span></li>'
					html+='</ul></div>'          
		            html+='<div class="classOpen">'     
		            html+='<span class="classTime font-12">销售时间：'+data.obj[0].salesStartDateString+'至'+data.obj[0].salesEndDateString+'</span>'       
		            if(data.obj[0].discountPrice==undefined||data.obj[0].discountPrice==null||data.obj[0].discountPrice==''){
		            	money=data.obj[0].price
		            }else{
		            	money=data.obj[0].discountPrice
		            }
					if(data.obj[0].isBuy=="Y"){
						html+='<span class="classStatus" style="width:100px;font-size:12px;background:#FFFBF2;color:#29A531;text-align:right">已购买</span>'     
					}else{
						html+='<span class="classStatus buy_classbtn" style="background:#FFAB2A;width:100px;font-size:12px;padding:0;box-shadow:none">购买本课程包</span>'     
					}
		            html+='</div></div>'
				}
				$(".baseDivs").append(html)
			}
		}
	});
	
}

//查询课程数据 预约跳转
function getCoursePageToSchool(){
	slf.ajax({
		url:'/system/getAlonCourseToCoursePageId',
		async:false,
		type:'get',
		param:{
			coursePageId:slf.getQueryStringByName('coursePage')
		},
		success:function(data){
			console.log(data)
			console.log("从预约过来查询课程")
			var courseHTML='';
			if(data.res==1&&data.obj.length>0){
				for(var a=0,b=data.obj.length;a<b;a++){
					if(data.obj[a].isVisible=="Y"){
						//展示可见课程
						courseHTML+='<li class="w100b am-margin-top-sm borB-EF borT-EF backpkgLi bg-FFFBF2" onclick="showBackDetail('+data.obj[a].courseId+')">'+
						'<ul class="am-avg-sm-3">'+
							'<li class="am-text-left baseName">'+data.obj[a].name+'</li>'+
							'<li class="am-text-center">适龄：'+data.obj[a].fitAge+'-'+data.obj[a].endAge+'岁</li>'
							/*if(data.obj[a].period==undefined||data.obj[a].period=="undefined"){*/
							courseHTML+='<li class="am-text-right" style="font-size:12px">学习周期:45分钟<i class="am-icon-angle-right am-icon-sm am-margin-left-xs"></i></li>';
							/*}else{
								courseHTML+='<li class="am-text-right">学习周期:'+data.obj[a].period+'周<i class="am-icon-angle-right am-icon-sm am-margin-left-xs"></i></li>';
							}*/
						courseHTML+='</ul>'+
						'</li>';
					}
					
					
				}
			}else{
				courseHTML+='<img src="../img/zwsj.png" alt="img" style="width:85%">'
			}
			$('#courseDetails').html(courseHTML);
		}
	});
	
}
function showBackDetail(courseId){
	console.log(courseId+","+coursePageIds)
	if(noSchoolId==1){
		window.location.href='basepkg_details.html?courseId='+courseId+'&order=y&coursePageIds='+coursePageIds+'&noSid=1'
	}else{
		window.location.href='basepkg_details.html?courseId='+courseId+'&order=y&coursePageIds='+coursePageIds+''
	}
}